package Servlet;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import DAO.SanPhamDAO;
import POJO.SanPham;

/**
 * Servlet implementation class ModifySanPhamServlet
 */
//@WebServlet("/ModifySanPhamServlet")
public class ModifySanPhamServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ModifySanPhamServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	    String maSanPham = request.getParameter("maSanPham");
	    SanPham sp = SanPhamDAO.SelectSanPhamById(maSanPham);
	    
	    request.setAttribute("sanPham", sp);	    
	    
	    RequestDispatcher dispatcher = request.getRequestDispatcher("/Modify/ModifySanPham.jsp");
        dispatcher.forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	    SanPham sanPham = new SanPham();
        if (request.getParameterValues("suaSanPham") != null)
        {
            String[] strs = request.getParameterValues("suaSanPham");

            // Chuyển sang chữ có dấu
            strs[0] = new String(strs[0].getBytes("ISO-8859-1"), "UTF-8");
            // Lấy tên
            sanPham.setTenSanPham(strs[0]);

            // Lấy ngày tháng năm
            String date = strs[2] + "/" + strs[1] + "/" + strs[3];
            sanPham.setNgayDang(date);

            strs[4] = new String(strs[4].getBytes("ISO-8859-1"), "UTF-8");
            sanPham.setMoTa(strs[4]);

            sanPham.setDonGia(Float.parseFloat(strs[5]));

            sanPham.setSoLuongToiThieu(Integer.parseInt(strs[6]));

            sanPham.setSoLuongTon(Integer.parseInt(strs[7]));

            strs[8] = new String(strs[8].getBytes("ISO-8859-1"), "UTF-8");
            sanPham.setTinhTrang(strs[8]);

            sanPham.setMaDanhMuc(Integer.parseInt(strs[9]));

            sanPham.setMaSanPham(Integer.parseInt(strs[10]));
            // Thêm sản phẩm
            SanPhamDAO.UpdateSanPhamById(sanPham);

            request.setAttribute("id", "2");
            
            ArrayList<SanPham> list = SanPhamDAO.SelectSanPhamALL();
            
            request.setAttribute("listSanPham", list);
            RequestDispatcher dispatcher = request.getRequestDispatcher("/index.jsp");
            dispatcher.forward(request, response);
        }
	}

}
